package com.rocksdata.mc.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ctz.commonframe.entity.ImageItem;
import com.ctz.commonframe.ui.BaseFragment;
import com.ctz.commonframe.ui.view.EmbeddedListView;
import com.ctz.commonframe.utils.GlideApp;
import com.ctz.commonframe.utils.PickImageUtil;
import com.rocksdata.mc.business.CampusAppManager;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.CampusAppInfo;
import com.rocksdata.mc.entity.StudentInfo;
import com.rocksdata.mc.entity.TeacherInfo;
import com.rocksdata.mc.ui.activity.FeedbackActivity;
import com.rocksdata.mc.ui.activity.LoginActivity;
import com.rocksdata.mc.ui.activity.MainActivity;
import com.rocksdata.mc.ui.activity.SettingsActivity;
import com.rocksdata.mc.ui.adapter.ApplicationListAdapter;
import com.rocksdata.mc.R;
import com.ctz.commonframe.ui.BottomMenuDialog;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2016/11/24.
 */
@ContentView(R.layout.fragment_me)
public class MeFragment extends BaseFragment {
    @ViewInject(R.id.tv_title_middle)
    private TextView tvTitle;
    @ViewInject(R.id.iv_head)
    private ImageView ivHead;
    @ViewInject(R.id.tv_name)
    private TextView tvName;
    @ViewInject(R.id.tv_belong_to)
    private TextView tvBelongTo;
    @ViewInject(R.id.tv_code)
    private TextView tvCode;
    @ViewInject(R.id.lv_me_application)
    private EmbeddedListView lvApp;
    @ViewInject(R.id.tv_login_or_logout)
    private TextView tvLoginOrLogout;
    private BottomMenuDialog addImageDialog;

    private List<CampusAppInfo> appList = new ArrayList<CampusAppInfo>();
    private ApplicationListAdapter adapter;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        tvTitle.setText(R.string.me);
        adapter = new ApplicationListAdapter(mContext, appList);
        lvApp.setAdapter(adapter);
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        boolean isVisitor = UserInfoManager.isVisitor();
        if(isVisitor){
            tvLoginOrLogout.setText(R.string.login);
            lvApp.setVisibility(View.GONE);
            tvName.setText(R.string.visitor);
            tvBelongTo.setText("");
        }else{
            tvLoginOrLogout.setText(R.string.logout);
            lvApp.setVisibility(View.VISIBLE);
            tvName.setText(UserInfoManager.getCurrentName());
            if(UserInfoManager.getCurrentUserType() == UserInfoManager.USER_TYPE_STUDENT){
                tvBelongTo.setText("班级：" + UserInfoManager.getClassName());
                if (UserInfoManager.getAvatar() != null) {
                    GlideApp.with(MeFragment.this).load(UserInfoManager.getAvatar()).error(R.mipmap.ic_default_avatar).centerCrop().into(ivHead);
                }
            }else if (UserInfoManager.getCurrentUserType() == UserInfoManager.USER_TYPE_TEACHER){
                tvBelongTo.setText("部门：" + UserInfoManager.getDepartmentName());
                if (UserInfoManager.getAvatar() != null) {
                    GlideApp.with(MeFragment.this).load(UserInfoManager.getAvatar()).error(R.mipmap.ic_default_avatar).centerCrop().into(ivHead);
                }
            }
        }

        getAppList();
    }

    private void getAppList(){
//        int type = userInfoManager.getCurrentUserType();
//        if(type == UserInfo.USER_TYPE_STUDENT){
//            appList.add(new CampusAppInfo("成绩单", R.mipmap.ic_app_report));
//            appList.add(new CampusAppInfo("德育", R.mipmap.ic_app_oa));
//        }else if(type == UserInfo.USER_TYPE_TEACHER){
//            appList.add(new CampusAppInfo("工资单", R.mipmap.ic_app_payroll));
//            appList.add(new CampusAppInfo("代办已办", R.mipmap.ic_app_oa));
//        }
//        appList.add(new CampusAppInfo("一卡通", R.mipmap.ic_app_campus_card));
//        appList.add(new CampusAppInfo("自画像", R.mipmap.ic_app_self_portrait));
//        List<CampusAppInfo> list = BusinessManager.getApplicationManager().getAppList(CampusAppInfo.MODULE_ME,
//                UserInfoManager.getCurrentUserType(), UserInfoManager.isVisitor(), true, false);
//        appList.clear();
//        appList.addAll(list);

        adapter.notifyDataSetChanged();
    }

    @Event(value = {R.id.lv_me_application}, type = AdapterView.OnItemClickListener.class)
    private void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.lv_me_application:
                CampusAppInfo info = adapter.getItem(position);
                CampusAppManager.skipToChildApplication(mContext, info);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        PickImageUtil.getInstance().handleActivityResult(requestCode, resultCode, data);
    }

    @Event(value = {R.id.layout_my_info, R.id.layout_settings, R.id.layout_feedback, R.id.tv_login_or_logout, R.id.iv_head})
    private void OnClick(View v) {
        switch (v.getId()){
            case R.id.layout_my_info:
//                if(!UserInfoManager.isVisitor())
//                    startActivity(new Intent(mContext, UserInfoActivity.class));
                break;
            case R.id.layout_settings:
                startActivity(new Intent(mContext, SettingsActivity.class));
                break;
            case R.id.layout_feedback:
                startActivity(new Intent(mContext, FeedbackActivity.class));
                break;
            case R.id.tv_login_or_logout:
                boolean isVisitor = UserInfoManager.isVisitor();
                if(!isVisitor){
                    BusinessManager.getAccountManager().logout();
                    mContext.finish();
                }
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_head:
                showAddImageDialog();
                break;
        }
    }

    private void showAddImageDialog() {
        if (addImageDialog == null) {
            addImageDialog = new BottomMenuDialog(mContext, BottomMenuDialog.TYPE_IMAGE_CAPTURE);
            addImageDialog.setOnMenu1ClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PickImageUtil.getInstance().selectImage(mContext, new PickImageUtil.OnPickImageResultListener() {
                        @Override
                        public void onResult(List<ImageItem> imageList) {
                            handleImagePicked(imageList.get(0));
                        }
                    });
                    addImageDialog.dismiss();
                }
            });
            addImageDialog.setOnMenu2ClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PickImageUtil.getInstance().takePhoto(mContext, new PickImageUtil.OnPickImageResultListener() {
                        @Override
                        public void onResult(List<ImageItem> imageList) {
                            handleImagePicked(imageList.get(0));
                        }
                    });
                    addImageDialog.dismiss();
                }
            });
        }
        addImageDialog.show();
    }

    private void handleImagePicked(final ImageItem item) {
        ((MainActivity)mContext).showLoadingDialog();
        BusinessManager.getAccountManager().uploadAvatar(item.imagePath, new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                ((MainActivity)mContext).dismissLoadingDialog();
                ((MainActivity)mContext).showToast(message);
                if (isSuccess) {
                    String url = (String) object;
                    GlideApp.with(MeFragment.this).load(url).centerCrop().diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(ivHead);
                }
            }
        });
    }

}
